package com.huluobo.user.page

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.user.R
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import com.huluobo.user.entity.ApproveEntity
import com.huluobo.user.entity.ApproveEntityItem
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/5/8.
 */

@Composable
fun ComApproveListPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<ApproveEntityItem>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        Log.e("TagA", "获取请假列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        list.clear()
                        val newList = it.data as ApproveEntity
                        list.addAll(newList.filter {
                            it.status == "未审核"
                        })
                    }
                }
            }
        }
        vm.sendIntent(UserIntent.GetApproves)
    })

    Column(Modifier.padding(10.dp)) {
        BaseTitle(title = "审批列表", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(10.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            var sort = false
            Text(text = "类型", modifier = Modifier.clickable {
                sort = if (sort) {
                    list.sortBy { it.type }
                    !sort
                } else {
                    list.sortByDescending { it.type }
                    !sort
                }
            })
            Text(text = "开始时间", modifier = Modifier.clickable {
                sort = if (sort) {
                    list.sortBy { it.startTime }
                    !sort
                } else {
                    list.sortByDescending { it.startTime }
                    !sort
                }
            })
            Text(text = "结束时间", modifier = Modifier.clickable {
                sort = if (sort) {
                    list.sortBy { it.endTime }
                    !sort
                } else {
                    list.sortByDescending { it.endTime }
                    !sort
                }
            })
            Text(text = "审批状态", modifier = Modifier.clickable {
                sort = if (sort) {
                    list.sortBy { it.status }
                    !sort
                } else {
                    list.sortByDescending { it.status }
                    !sort
                }
            })
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list) {
                ApproveItem(entityItem = it, navHostController)
            }
        })
    }
}